/*
    通用网络请求
 */
class NetClient {
    constructor() {
        this.http = new XMLHttpRequest();
    }
    /*
    requestURL
    url: 要请求的地址
    method: get or post
    params: json格式的参数列表
    onload: 回调方法
 */
    requestUrl(url, method, params, onload) {
        this.http.open(method, url, true);
        params = this.addCommonParams(params);
        console.log("请求参数" + JSON.stringify(params));
        this.http.setRequestHeader('content-type', 'application/json; charset=UTF-8');
        this.http.onreadystatechange = ()=>{
            if (this.http.readyState ===XMLHttpRequest.DONE
                && this.http.status === 200) {
                var data = JSON.parse(this.http.responseText);
                onload(null, data);
            }
            else {
                onload('Error: ${this.http.status}');
            }
        };
        this.http.send(JSON.stringify(params));
    }

    addCommonParams(params) {
        params.time = Date.now();
        return params;
    }
}
